import axios from 'axios'
import router from '../router'
const qs = require('qs');

 var baseUrl = "http://127.0.0.1:10090";
//var baseUrl = "http://128.64.151.123:10090";
 var baseUrl2 = "http://127.0.0.1:8080/metro_support_manage/";
//var baseUrl2 = "http://128.64.151.123:8080/metro_support_manage/";

export const http = axios.create({
    baseURL: '/api',
    // timeout: 10000
    headers:{'Content-Type':'application/x-www-form-urlencoded'},
    transformRequest:[function (data){
        data = qs.stringify(data, {arrayFormat: 'brackets'});
        return data;
    }],
});

export const req = axios.create({
    baseURL: '/api',
    headers:{'Content-Type':'application/json;charset=utf-8'},
});




http.interceptors.request.use(
    config => {
        // config.headers['Content-Type'] = 'application/x-www-form-urlencoded';
        const token = sessionStorage.getItem('access_token');
        if (token) {  // 为每次请求添加token
            config.headers.Authorization = token;
        }
        return config;
    },
    error => {
        return Promise.reject(error);
    });


    http.interceptors.response.use(
    response => {
        if (response.headers.access_token) {
            sessionStorage.setItem('access_token', response.headers.access_token);
        }
        return response;
    },
    error => {
        if (error.response) {
            if (error.response.headers.access_token) {
                sessionStorage.setItem('access_token', error.response.headers.access_token);
            }
            switch (error.response.status) {
                case 401:
                    // 清除token
                    sessionStorage.removeItem('access_token');
                    router.replace({
                        path: 'login',
                        //登录成功后跳入浏览的当前页面
                        // query: { redirect: router.currentRoute.fullPath }
                    })
                case 403:
                    router.replace({
                        path: '/403',
                    })
            }
        }
        return Promise.reject(error)
    });


    req.interceptors.request.use(
        config => {
            // config.headers['Content-Type'] = 'application/x-www-form-urlencoded';
            const token = sessionStorage.getItem('access_token');
            if (token) {  // 为每次请求添加token
                config.headers.Authorization = token;
            }
            return config;
        },
        error => {
            return Promise.reject(error);
        });
    
    
    req.interceptors.response.use(
        response => {
            if (response.headers.access_token) {
                sessionStorage.setItem('access_token', response.headers.access_token);
            }
            return response;
        },
        error => {
            if (error.response) {
                if (error.response.headers.access_token) {
                    sessionStorage.setItem('access_token', error.response.headers.access_token);
                }
                switch (error.response.status) {
                    case 401:
                        // 清除token
                        sessionStorage.removeItem('access_token');
                        router.replace({
                            path: 'login',
                            //登录成功后跳入浏览的当前页面
                            // query: { redirect: router.currentRoute.fullPath }
                        })
                    case 403:
                        router.replace({
                            path: '/403',
                        })
                }
            }
            return Promise.reject(error)
        });



    export default http;
